#delimit;
set more off;

set mem 500m;

set logtype text;

log using /home/dnc2101/Accidental_Deaths/Vital_Stats/Summary_Stats_Types_Accidents/make_drug_OD_vars.log, replace;



/********************************************************************************************************************/
*
* This do file takes the Vital Stats Mortality data, and creates a dataset that has a count of the number of fatal 
* drug overdoses in occur in each state in each year.  These counts can be used along with Census data on 
* state-year populations to generate the rate of fatal drug overdoses that occurs in each state in each year.  This 
* do file creates these counts for the years 1981 through 1998, the years of the data that use ICD-9 coding on cause 
* of death;  a seperate do file does the same exact task for the years 1999 through 2004, the years of data which 
* use ICD-10 coding on cause of death
*
*
* Program by Dan Carvell, written between Fall 2008 and Spring 2010.  
*
*********************************************************************************************************************/








use "/home/dnc2101/Accidental_Deaths/Vital_Stats/Data_For_Appending/Injury_Deaths_81_98.dta", clear;


gen drug_OD=0;
replace drug_OD=1 if ucr282==31700;

label variable drug_OD "=1 if death is a drug overdose";





collapse (sum) drug_OD, by(fipsstr datayear); 


** sort the data so that the merge with other data goes OK;

sort fipsstr datayear;


save "/home/dnc2101/Accidental_Deaths/Vital_Stats/Cleaned_Datasets/Drug_OD_Counts_81_98.dta", replace;

** export this into xml format so I can put the count of drug overdoses into the raw excel data by hand, and browse this data  ;
** for comparison with data on all types of fatal poisonings, not just drug overdoses, from the CDC's WISQARs dataset  ;

xmlsave "/home/dnc2101/Accidental_Deaths/Vital_Stats/Cleaned_Datasets/Drug_OD_Counts_81_98.xml", doctype(excel) replace;










** Now redo this process, but with collapsing at the year level rather than state-year level, to get a national-level rate for a figure for the paper;


use "/home/dnc2101/Accidental_Deaths/Vital_Stats/Data_For_Appending/Injury_Deaths_81_98.dta", clear;


gen drug_OD=0;
replace drug_OD=1 if ucr282==31700;

label variable drug_OD "1 if death is a drug overdose";



collapse (sum) drug_OD, by(datayear); 


** sort the data so that the merge with other data goes OK;

sort datayear;


save "/home/dnc2101/Accidental_Deaths/Vital_Stats/Cleaned_Datasets/Drug_OD_Counts_81_98_National.dta", replace;

** export this into xml format so I can put the count of drug overdoses into the raw excel data by hand;

xmlsave "/home/dnc2101/Accidental_Deaths/Vital_Stats/Cleaned_Datasets/Drug_OD_Counts_81_98_National.xml", doctype(excel) replace;




log close;










